"use strict";

module.exports = {
  /**
   * validate data with rules
   *
   * @param  {Object/Array} rules  - validate rule object
   * @param  {Object} [data] - validate target, default to `params,query,body`
   * @param  {Boolean} [mergeError]
   */
  validate(rules, data, mergeError) {
    if (typeof data === "boolean") {
      mergeError = data;
    }
    data =
      typeof data === "boolean" || !data
        ? Object.assign(
            {},
            this.request.params,
            this.request.queries,
            this.request.body
          )
        : data;
    const res = this.app.fishValidator.validate(rules, data, mergeError);
    if (res.errors || (mergeError === false && res.code === "invalid")) {
      this.throw(422, res);
    } else {
      return res;
    }
  },
};
